﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using Infrastructure;
using Moq;
using RegCustomer;

namespace SpecProj
{

    public class PublishedEventArgs : EventArgs
    {
        public Event Event { get; set; }
    }

    public class FakePublisher : Infrastructure.IEventPublisher
    {
        public event EventHandler Published;
        public void Publish<T>(T @event) where T : Event
        {
            if (this.Published != null)
            {
                PublishedEventArgs args = new PublishedEventArgs();
                args.Event = @event;
                Published(this, args);
            }
        }
        public void RegisterSubscriber(FakeSubscriber subscriber)
        {
            this.Published += new EventHandler(subscriber.Handle);
        }
    }

    public class FakeSubscriber
    {
        public Dictionary<String, Event> HandledEvents { get; private set; }
        public FakeSubscriber()
        {
            HandledEvents = new Dictionary<string, Event>();
        }
        public void Handle(object sender, EventArgs eventArgs)
        {
            HandledEvents.Add(((PublishedEventArgs)eventArgs).Event.ToString(),
                                ((PublishedEventArgs)eventArgs).Event);
        }
    }


    [Binding]
    public class Setup
    {
        protected CreateNewCustomerCommand _createNewCustomer;
        protected UpdateCustomerCommand _updateNewCustomer;
        protected DeactivateCustomerCommand _invalidateCustomerCommand;
        protected MakeCustomerPriorityCommand _priorityCustomerCommand;

        protected string _name = "Sean";
        protected Guid _id;

        protected Repository<Customer> _repository;
        protected EventStore _eventStore;
        //protected Mock<IEventPublisher> _publisher;
        protected CommandHandlers _handlers;
        protected FakeBus _bus = new FakeBus();

        protected FakePublisher _fakePublisher;
        protected FakeSubscriber _fakeSubscriber;

        protected int _version;

        [BeforeScenario]
        public void SetupInfrastructure()
        {
            _fakePublisher = new FakePublisher();
            _eventStore = new EventStore(_fakePublisher);
            _repository = new Repository<Customer>(_eventStore);
            _handlers = new CommandHandlers(_repository);
            _fakeSubscriber = new FakeSubscriber();

            _fakePublisher.RegisterSubscriber(_fakeSubscriber);

            _bus.RegisterHandler<CreateNewCustomerCommand>(_handlers.Handle);
            _bus.RegisterHandler<UpdateCustomerCommand>(_handlers.Handle);
            _bus.RegisterHandler<DeactivateCustomerCommand>(_handlers.Handle);
            _bus.RegisterHandler<MakeCustomerPriorityCommand>(_handlers.Handle);

            StateMachine.States.Clear();
            StateMachine.SetupStateMachineFromXml(
            @"C:\Users\Sean\Desktop\Thesis\Sample Projects\reg-customer\RegCustomer\StateTransferFunction.xml"
                );

            _id = Guid.NewGuid();
            _version = 0;
        }

        [AfterScenario]
        public void AfterScenario()
        {
            _fakeSubscriber.HandledEvents.Clear();
            StateMachine.States.Clear();
            Console.WriteLine("--------------");
            Console.WriteLine("");
        }


    }
}
